﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UCS.ArtificialInteligence.AI
{
    public class AStarSearch
    {
        #region Public Methods

        public IProblemState Find(IProblem problem)
        {
            var list = new AStarList();
            var exploredStates = new List<IProblemState>();

            list.Add(problem.InitialState.HeuristicValue, 0, problem.InitialState);
            var currentNode = list.Remove();
            var currentState = currentNode.State;

            while (!problem.IsFinalState(currentState))
            {
                exploredStates.Add(currentState);

                var borders = currentState.Borders;

                foreach (var border in borders)
                    if (!list.ContainsState(border.State) && !exploredStates.Contains(border.State))
                        list.Add(border.Weight + border.State.HeuristicValue, currentNode.Depth + 1, border.State);

                currentNode = list.Remove();
                currentState = currentNode.State;
            }

            return currentState;
        }

        #endregion
    }
}
